package com.sinothk.image.recorder.demo;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.cjt2325.cameralibrary.util.DeviceUtil;
import com.sinothk.image.recorder.CameraActivity;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    private ImageView photo;
    private TextView device;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CameraActivity.start(MainActivity.this, 100, true);
            }
        });
        photo = (ImageView) findViewById(R.id.image_photo);
        device = (TextView) findViewById(R.id.device);
        device.setText(DeviceUtil.getDeviceInfo());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 101) {
            Log.i("CJT", "picture");
            String path = data.getStringExtra("path");
            photo.setImageBitmap(BitmapFactory.decodeFile(path));
        }
        if (resultCode == 102) {
            Log.i("CJT", "video");
            String path = data.getStringExtra("path");
            String videoUrl = data.getStringExtra("videoUrl");
            photo.setImageBitmap(BitmapFactory.decodeFile(path));
            File file = new File(videoUrl);
            Toast.makeText(this, "fileSize = " + file.length(), Toast.LENGTH_SHORT).show();
        }
        if (resultCode == 103) {
            Toast.makeText(this, "请检查相机权限~", Toast.LENGTH_SHORT).show();
        }
    }
}
